package com.sigmazero13.gameutils;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.sigmazero13.gameutils.model.GameInfo;
import com.sigmazero13.gameutils.model.GamePlayInfo;
import com.sigmazero13.gameutils.model.PlayReport;
import com.sigmazero13.gameutils.model.WinLossInfo;
import com.sigmazero13.gameutils.utils.PlayReportBuilder;
import com.sigmazero13.gameutils.utils.PlayReportBuilder.ReportVariable;

public class PlayReportView extends Activity {
	private GameInfo game;
	
	private PlayReportBuilder builder;
	private Spinner rowSpinner;
	private Spinner colSpinner;
	
	@Override
	public void onCreate(Bundle savedStateInstance) {
		super.onCreate(savedStateInstance);
		setContentView(R.layout.play_report);
		
		Bundle bundle = getIntent().getExtras();
		Integer gameId = bundle.getInt("game_id");
		
		game = ((BoardGameUtilsApp)getApplication()).getDbHelper().selectSingleGame(gameId);	
		
		List<GamePlayInfo> plays = ((BoardGameUtilsApp)getApplication()).getDbHelper().getPlaysForGame(game.getGameId(), true);
		builder = new PlayReportBuilder(plays);
		
		ArrayAdapter<ReportVariable> adapter = new ArrayAdapter<ReportVariable>(this, android.R.layout.simple_spinner_item, PlayReportBuilder.ReportVariable.values());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		rowSpinner = (Spinner)findViewById(R.id.report_row_type);
		rowSpinner.setAdapter(adapter);
		rowSpinner.setSelection(0);
		colSpinner = (Spinner)findViewById(R.id.report_col_type);
		colSpinner.setAdapter(adapter);
		colSpinner.setSelection(1);
		
		Button generate = (Button)findViewById(R.id.generate_report_button);
		generate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				createReport();
			}
		});
		
		createReport();
	}
	
	private void createReport() {
		ReportVariable rowType = (ReportVariable)rowSpinner.getSelectedItem();
		ReportVariable colType = (ReportVariable)colSpinner.getSelectedItem();
		if (!PlayReportBuilder.isValidReportType(rowType, colType)) {
			Toast.makeText(this, R.string.win_report_invalid, Toast.LENGTH_SHORT).show();
		} else {
			PlayReport report = builder.buildReport(rowType, colType);
			buildTable(report);
		}
	}
	
	private void buildTable(PlayReport report) {
		TableLayout reportTable = (TableLayout)findViewById(R.id.play_report_table);
		
		reportTable.removeAllViews();
		
		TableRow headerRow = new TableRow(this);
		headerRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		headerRow.addView(new TextView(this));
		for (String colKey : report.getColHeadings()) {
			if (colKey.equals("TOTAL"))
				continue;
			headerRow.addView(buildHeaderCell(colKey, Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM));
		}
		if (report.getColHeadings().contains("TOTAL"))
			headerRow.addView(buildHeaderCell("TOTAL", Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM));
		
		reportTable.addView(headerRow);
		
		for (String rowKey : report.getRowHeadings()) {
			if (rowKey.equals("TOTAL"))
				continue;
			reportTable.addView(buildRow(report, rowKey));
		}
		if (report.getRowHeadings().contains("TOTAL"))
			reportTable.addView(buildRow(report, "TOTAL"));
	}
	
	private TableRow buildRow(PlayReport report, String rowKey) {
		TableRow tr = new TableRow(this);
		tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		tr.setGravity(Gravity.LEFT);
		
		tr.addView(buildHeaderCell(rowKey, Gravity.CENTER_VERTICAL|Gravity.RIGHT));
		
		for (String colKey : report.getColHeadings()) {
			if (colKey.equals("TOTAL"))
				continue;
			
			tr.addView(buildCell(report, rowKey, colKey));
		}
		if (report.getColHeadings().contains("TOTAL"))
			tr.addView(buildCell(report, rowKey, "TOTAL"));
		
		return tr;
	}
	
	private View buildHeaderCell(String key, int gravity) {
		TextView tv = new TextView(this);
		tv.setText(key);
		tv.setGravity(gravity);
		tv.setTextAppearance(this, R.style.table_heading);
		tv.setPadding(15, 15, 0, 0);
		
		return tv;
	}
	
	private View buildCell(PlayReport report, String rowKey, String colKey) {
		WinLossInfo info = report.getCellInfo(rowKey, colKey);
		TextView tv = new TextView(this);
		SpannableStringBuilder sb  = new SpannableStringBuilder();
		if (info != null && !(info.getWins() == 0 && info.getLosses() == 0)) {
			String text = info.getWins() + " - ";
			int lossStart = text.length();
			text = text + info.getLosses();
			sb.append(text);
			sb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.win_color)), 0, info.getWins().toString().length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
			sb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.loss_color)), lossStart, text.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		} else {
			sb.append("-");
		}
		
		tv.setText(sb, TextView.BufferType.NORMAL);
		tv.setPadding(15, 15, 0, 0);
		tv.setGravity(Gravity.CENTER);
		
		return tv;		
	}
}
